<?php
	$this->load->helper('MY_app');
	$this->load->helper('navigation/navigation');
	// Setup our default assets to load.
	Assets::add_css(array('css/bootstrap.css',));
	Assets::add_js( array(
		Template::theme_url('js/jquery-1.6.4.min.js'),
		Template::theme_url('js/bootstrap-dropdown.js'),
		Template::theme_url('js/bootstrap-alerts.js'),
	), 'external', true);
?>
<!doctype html>
<html lang="en">
<head>
	<meta charset="UTF-8" />
	
	<title><?php echo config_item('site.title'); ?></title>
	
	<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">

	<?php echo Assets::css(); ?>
	
	<?php echo Assets::external_js('head.min.js'); ?>
</head>
<body>

	<div class="topbar">
		<div class="fill">
			<div class="container">
			<a class="brand" href="<?php echo base_url() ?>"><?php echo $this->settings_lib->item('site.title') ?></a>
			
			<?php
			// print navigation
			$attributes['class']  = 'nav';
			echo show_navigation('main', TRUE, $attributes);
			?>
			
			<?php
			// acessing our userdata cookie
			$cookie = unserialize($this->input->cookie($this->config->item('sess_cookie_name')));
			$logged_in = isset ($cookie['logged_in']);
			unset ($cookie);
			?>
				
			<?php if($logged_in){ ?>
			<ul class="nav secondary-nav">
				<li><a href="<?php echo base_url().'admin' ?>">Admin Panel</a></li>
				<li class="dropdown" data-dropdown="dropdown">
					<a href="#" class="dropdown-toggle"><?php echo $this->settings_lib->item('auth.use_usernames') ? ($this->settings_lib->item('auth.use_own_names') ? $this->auth->user_name() : $this->auth->username()) : $this->auth->email() ?></a>
					<ul class="dropdown-menu">
						<li><a href="<?php echo site_url('users/profile');?>">Edit Profile</a></li>
						<li class="divider"></li>
						<li><a href="<?php echo site_url('logout');?>">Logout</a></li>
					</ul>
				</li>
			</ul>
			<?php }else{ ?>
			<form action="" class="pull-right">
				<input class="input-small" type="text" placeholder="Username">
				<input class="input-small" type="password" placeholder="Password">
				<button class="btn" type="submit">Sign in</button>
			</form>
			<?php } ?>
			</div>
		</div>
		<div id="message"><?php echo Template::message(); ?></div>
	</div>
	
	<header class="jumbotron subhead" id="overview">
		<div class="inner">
			<div class="container">
				<h1><?php echo $this->settings_lib->item('site.title') ?></h1>
				<p class="lead">Site description here...</p>
				<p>More explanation text if needed.</p>
			</div><!-- /container -->
		</div>
    </header>

	<div class="container">

		<div class="content">
			<?php if (isset($page_title)) { ?>
			<div class="page-header">
				<h1><?php echo $page_title ?> <small></small></h1>
			</div>
			<?php } ?>
			<div class="row">
				<div class="span12">
					<?php echo isset($content) ? $content : Template::yield(); ?>
				</div>
				<div class="span4">
					<?php Template::block('secondary_content', ''); ?>
				</div>
			</div>
		</div>

	</div> <!-- /container -->
	
	<footer>
		<div class="container">
			<?php if (ENVIRONMENT == 'development') :?>
			<p class="pull-right">Page rendered in {elapsed_time} seconds, using {memory_usage}.</p>
			<?php endif; ?>

			<p>Powered by <a href="http://id.linkedin.com/in/asatrya" target="_blank">Aditya Satrya </a></p>
		</div>
	</footer>
	
	<div id="debug"></div>
	
	<script>
		head.js(<?php echo Assets::external_js(null, true) ?>);
		head.js(<?php echo Assets::module_js(true) ?>);
	</script>
	<?php echo Assets::inline_js(); ?>
</body>
</html>

<?php

function bootstrap_context_nav($breakdown_context = array('content'), $exclude_context = array('settings', 'reports', 'developer'))
{ 
	$contexts = context_nav_arr();
	
	/*
		Build out our navigation.
	*/
	$nav = '';
	foreach ($contexts as $key => $context)
	{	
		if(in_array($key, $exclude_context)){
			continue;
		}
		
		if (has_permission('Site.'. ucfirst($key) .'.View'))
		{	
			$url = $context['url'];
			$class = "class='dropdown-toggle'";
			$id = $context['id'];
			$title = $context['title'];
			$subnav_arr = context_subnav_arr($key);
			//dump($subnav_arr);
			
			if(in_array($key, $breakdown_context)){
				$nav .= build_menu2($key, $subnav_arr);
			}else{
				$nav .= "<li class='dropdown' data-dropdown='dropdown'><a href='{$url}' {$class} id='{$id}' title='{$title}'>";
				$nav .= $title;
				$nav .= "</a>";
				$nav .= build_menu($key, $subnav_arr);
				$nav .= "</li>";
			}
		}
	}
	
	return $nav;
}

/*
	Method: build_menu()
	
	Handles building out the HTML for the menu.
	
	Parameters:
		$context	- The context to build the menu for.	
*/
function build_menu($context, $menu) 
{
	// Build a ul to return
	$list = "<ul class='dropdown-menu'>\n";
	
	//echo '<pre>'; die(print_r($this->menu));
	foreach ($menu as $topic_name => $modules)
	{		
		// If the modules has other items, we're not closed.
		$closed = true;
		
		// If there is more than one item in the modules, we need to build
		// out a menu based on the multiple items.
		if (count($modules) > 1)
		{
			$class = '';
		
			reset($modules);
			$first_key = key($modules);
			$list .= '<li><a href="#" class="dropdown-toggle">'. ucwords($modules[$first_key]["menu_topic"]) .'</a>';
			$list .= '<ul class="dropdown-item">';
			
			foreach ($modules as $module => $vals)
			{ 	
				$class = '';
			
				$list .= build_item($module, $vals['title'], $vals['display_name'], $context, $vals['menu_view']);
			}
				
			$list .= '</ul></li>';
		}
		else
		{
			foreach ($modules as $module => $vals)
			{ 
				$list .= build_item($module, $vals['title'], $vals['display_name'], $context, $vals['menu_view']);
			}
		}
		
	}
	
	$list .= "</ul>\n";
	
	return $list;
}

function build_menu2($context, $menu) 
{
	// Build a ul to return
	$list = "";
	
	//echo '<pre>'; die(print_r($this->menu));
	foreach ($menu as $topic_name => $modules)
	{		
		// If the modules has other items, we're not closed.
		$closed = true;
		
		// If there is more than one item in the modules, we need to build
		// out a menu based on the multiple items.
		if (count($modules) > 1)
		{
			$class = '';
		
			reset($modules);
			$first_key = key($modules);
			$list .= '<li><a href="#" class="dropdown-toggle">'. ucwords($modules[$first_key]["menu_topic"]) .'</a>';
			$list .= '<ul class="dropdown-item">';
			
			foreach ($modules as $module => $vals)
			{ 	
				$class = '';
			
				$list .= build_item($module, $vals['title'], $vals['display_name'], $context, $vals['menu_view']);
			}
				
			$list .= '</ul></li>';
		}
		else
		{
			foreach ($modules as $module => $vals)
			{ 
				$list .= build_item($module, $vals['title'], $vals['display_name'], $context, $vals['menu_view']);
			}
		}
		
	}
	
	
	return $list;
}

//--------------------------------------------------------------------


/*
	Method: build_item()
	
	Handles building an individual list item (with sub-menus) for the menu.
	
	Parameters:
		$module			- The name of the module this link belongs to
		$title			- The title used on the link
		$display_name	- The name to display in the menu
		$context		- The name of the context
		$menu_view		- The name of the view file that contains the sub-menu
		
	Returns:
		The HTML necessary for a single item and it's sub-menus.
*/
function build_item($module, $title, $display_name, $context, $menu_view='') 
{
	// Is this the current module? 	
	$class = '';
	
	$item  = '<li><a href="'. site_url(SITE_AREA .'/'. $context .'/'. $module) .'" '. $class;
	$item .= ' title="'. $title .'">'. ucwords(str_replace('_', '', $display_name)) ."</a>\n";
	
	$item .= "</li>\n";
			
	return $item;
}

//--------------------------------------------------------------------